#!/bin/sh
#抽取ODS层的0200与0f39故障数据到dwc_lc_faultinfo_pdf表中
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE-$0}")"; pwd)
UP_DIR=$(dirname $(dirname $(dirname "$PWD")))
echo ${UP_DIR}
source ${UP_DIR}/system_env.sh


START_TIME=$(date +%s)
hive_db=${hive_db}
hive_db_source=${hive_db_source}

if [ -z "${hive_db}" ];then
  hive_db=dataplatform_trip
else
  hive_db=${hive_db}
fi

if [ -z "${hive_db_source}" ];then
  hive_db_source=dongfeng
else
  hive_db_source=${hive_db_source}
fi


if [ -z "$1" ];then
  part_time=$(date -d "1 days ago" +%Y%m%d)
else
  part_time=$1
fi

sql_suffix=$(cat <<EOF
 insert overwrite table ${hive_db}.dwc_lc_faultinfo_pdf partition (part_time)
 select tid, protocol,gpstime,originallng,originallat,
    longitude,latitude,height,direction,speed,faultAddr,spn,fmi,faultType,subSpeed,accelerator,
    brake,rotation,engineTurbochargedPressure,engineIntakePressure,engineExhaustTemperature,
    engineCoolantTemperature,accelerationRate,gear,engineOutputTorque,
    load,engineLoad,vehicleAcceleration,vehicleSpeedReduction,faultCodeCount,part_time
from(
    select s.tid,"0f39"as protocol,s.gpstime,cast(s.originallng as string)as originallng,cast(s.originallat as string)as originallat,
    s.longitude,s.latitude,s.height,s.direction,s.speed,f.faultAddr,f.spn,f.fmi,f.faultType,f.subSpeed,f.accelerator,
    f.brakeSignal as brake,f.rotation,f.engineTurbochargedPressure,f.engineIntakePressure,f.engineExhaustTemperature,
    f.engineCoolantTemperature,f.accelerationRate,f.gear,f.engineOutputTorque,
    f.load,f.engineLoad,f.vehicleAcceleration,f.vehicleSpeedReduction,f.faultCodeCount,s.part_time
    from  ${hive_db_source}.navinfo_parquet_0f39 s
    lateral view ${hive_db}.unpack_fault(s.faultlist) f
    where s.part_time=${part_time} and s.faultlist!='[]'
    )t distribute by part_time;
EOF
)

CUR_DIR=$(cd "$(dirname "$0")"; pwd)
hive_config=$(cat  "${CUR_DIR}"/hive.config)
sql="${hive_config}${sql_suffix}"
echo "导入dwc_lc_faultinfo_pdf的SQL为：${sql}"

#执行语句·
hive -e "${sql}"
if [ $? -ne 0 ];then
  echo "导入dwc_lc_faultinfo_pdf失败，耗时$((`date +%s`-START_TIME))秒..."
else
  echo "导入dwc_lc_faultinfo_pdf成功，耗时$((`date +%s`-START_TIME))秒..."
fi